import { http, HTTP_200 } from '@/store/api'

const state = {
  autoWords: []
}

const getters = {
}

const mutations = {
  setWordList (state, payload) {
    state.autoWords = payload
  },
  removeWord (state, payload) {
    state.autoWords = state.autoWords.filter(w => w.id === payload.id)
  },
  clearList (state) {
    state.autoWords = []
  }
}

const actions = {
  autoList ({commit}, payload) {
    http.get(`/dict/auto/?key=${payload}`).then(res => {
      if (res.status === HTTP_200) {
        commit('setWordList', res.data)
      }
    }).catch(err => {
      console.log(err)
    })
  },
  searchWord ({commit}, payload) {
    http.get(`/dict/?word=${payload}`).then(res => {
      commit('', res.data)
    }).catch(err => {
      console.log(err)
    })
  }
}

export default {
  state,
  getters,
  mutations,
  actions
}
